package com.bysj.yqsht.web.controller;

import com.bysj.yqsht.bean.Student;
import com.bysj.yqsht.bean.extend.StudentExtend;
import com.bysj.yqsht.service.IStudentService;
import com.bysj.yqsht.utils.CustomerException;
import com.bysj.yqsht.utils.Message;
import com.bysj.yqsht.utils.MessageUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
@Api(description = "学生管理接口")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @GetMapping("findAll")
    public Message findAll(){
        List<Student> Student = studentService.findAll();
        return MessageUtil.success(Student);
    }


    @GetMapping("findAllWithClass")
    public Message findAllWithClass(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int limit){
        List<StudentExtend> StudentExtend = studentService.findAllWithClass(pageNum,limit);
        return MessageUtil.success(StudentExtend);
    }

    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Student student){
//        if (student.getName()==null){
//            return MessageUtil.error("错误：学生姓名不可为空");
//        }
//        int i = studentService.selectDataCount(student);
//        if (i!=0){
//            if (student.getId() == null){
//                return MessageUtil.error("错误：学生姓名已存在");
//            } else
//          //      if (i == 1 && student.getId() != null){
//                studentService.saveOrUpdate(student);
//                return MessageUtil.success("修改成功");
//           // }
//        }
        studentService.saveOrUpdate(student);
        return MessageUtil.success("保存成功");
    }
//    @PostMapping("update")
//    public Message update(Student student){
//        studentService.update(student);
//        return MessageUtil.success("修改成功");
//    }
    @GetMapping("deleteById")
    public Message deleteById(Long id){
        studentService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @GetMapping("findWithAllByName")
    public Message findWithByNumber(String name, String number,Long classId){
        List<StudentExtend> StudentExtend = studentService.findWithAllByName(name,number,classId);
        return MessageUtil.success(StudentExtend);

    }



}

//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;

//@RestController
//@RequestMapping("/article")
//public class StudentController{
//    @GetMapping("findById")
//    private String findBuId(){
//        return "这是一个文章";
//    }
//
//}


